﻿// HSS.ServiceModel.TimeoutSettings.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       TimeoutSettings.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.ServiceModel
{
	#region Using Directives
	using System;
	using System.ServiceModel;
	using HSS.ServiceModel.Internal;
	#endregion

	#region TimeoutSettings
	/// <summary>
	/// Contains the default communication timeouts used by VirtualChannels and VirtualOperations.
	/// </summary>
	public class TimeoutSettings : IDefaultCommunicationTimeouts
	{
		#region Defaults
		internal static TimeSpan DefaultCloseTimeout = TimeSpan.FromTicks(0x23c34600L * 1);
		internal static TimeSpan DefaultOpenTimeout = TimeSpan.FromTicks(0x23c34600L * 1);
		internal static TimeSpan DefaultReceiveTimeout = TimeSpan.FromTicks(0x23c34600L * 10);
		internal static TimeSpan DefaultSendTimeout = TimeSpan.FromTicks(0x23c34600L * 1);
		#endregion

		#region Constructors
		/// <summary>
		/// Constructor
		/// </summary>
		public TimeoutSettings()
		{
			this.CloseTimeout = DefaultCloseTimeout;
			this.OpenTimeout = DefaultOpenTimeout;
			this.ReceiveTimeout = DefaultReceiveTimeout;
			this.SendTimeout = DefaultSendTimeout;
		}
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the close timeout.
		/// </summary>
		public TimeSpan CloseTimeout { get; private set; }
		/// <summary>
		/// Gets or sets the open timeout.
		/// </summary>
		public TimeSpan OpenTimeout { get; private set; }
		/// <summary>
		/// Gets or sets the Receive timeout.
		/// </summary>
		public TimeSpan ReceiveTimeout { get; private set; }
		/// <summary>
		/// Gets or sets the send timeout.
		/// </summary>
		public TimeSpan SendTimeout { get; private set; }
		#endregion

		#region Methods
		/// <summary>
		/// Gets a new instance with the default or desired values.
		/// </summary>
		/// <remarks>
		/// <para>
		/// int sendTimeout = 1, int closeTimeout = 1, int openTimeout = 1, int receiveTimeout = 10
		/// </para>
		/// </remarks>
		/// <returns>A new TimeoutSettings instance.</returns>
		public static TimeoutSettings Default()
		{
			return new TimeoutSettings
				{
					CloseTimeout = FromMinutes(1),
					OpenTimeout = FromMinutes(1),
					ReceiveTimeout = FromMinutes(10),
					SendTimeout = FromMinutes(1)
				};
		}
		/// <summary>
		/// Gets a new custom instance with the desired values.
		/// </summary>
		/// <param name="sendTimeout">The desired send timeout in minutes.</param>
		/// <param name="closeTimeout">The desired close timeout in minutes.</param>
		/// <param name="openTimeout">The desired open timeout in minutes.</param>
		/// <param name="receiveTimeout">The desired receive timeout in minutes.</param>
		/// <returns>A new TimeoutSettings instance.</returns>
		public static TimeoutSettings Custom(int sendTimeout, int closeTimeout, int openTimeout, int receiveTimeout)
		{
			return new TimeoutSettings
			{
				CloseTimeout = FromMinutes(closeTimeout),
				OpenTimeout = FromMinutes(openTimeout),
				ReceiveTimeout = FromMinutes(receiveTimeout),
				SendTimeout = FromMinutes(sendTimeout)
			};
		}
		static TimeSpan FromMinutes(int minutes)
		{
			return TimeSpan.FromTicks(0x23c34600L * minutes);
		}
		#endregion
	}
	#endregion
}